import { api } from "../api"

export async function fetchAreaActivityTagsApi(areaId: string) {
  try {
    const response = await api.get('/java/ground/aix/activity/get_activity_tags', {
      params: { areaId },
    });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区活动标签失败')
  }
}

export async function fetchAreaActivitiesApi(params: any) {
  try {
    const response = await api.get('/java/ground/aix/activity/get_activities', {
      params,
    });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区活动标签失败')
  }
}

export async function fetchAreaCoursesTagsApi(areaId: string) {
  try {
    const response = await api.get('/java/ground/aix/course/get_course_tags', {
      params: { areaId },
    });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区课程标签失败')
  }
}

export async function fetchAreaCoursesApi(params: any) {
  try {
    const response = await api.get('/java/ground/aix/course/get_courses', { params });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区课程失败')
  }
}

export async function fetchAreaProjectTagsApi(areaId: string) {
  try {
    const response = await api.get('/java/ground/aix/project/get_project_tags', {
      params: { areaId },
    });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区项目标签失败')
  }
}

export async function fetchAreaProjectsApi(params: any) {
  try {
    const response = await api.get('/java/ground/aix/project/get_projects', { params });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区项目失败')
  }
}

export async function fetchAreaSceneTagsApi(areaId: string) {
  try {
    const response = await api.get('/java/ground/aix/scene/get_scene_tags', {
      params: { areaId },
    });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区场景标签失败')
  }
}

export async function fetchAreaScenesApi(params: any) {
  try {
    const response = await api.get('/java/ground/aix/scene/get_scenes', { params });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区微场景失败')
  }
}

export async function fetchUserDetailApi(params: any) {
  try {
    const response = await api.get('/java/ground/aix/area/get_all_scene_authors', { params });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区用户详情失败')
  }
}

export async function fetchSceneAuthorInfoApi(params: any) {
  try {
    const response = await api.get('/java/ground/aix/area/get_scene_author_info', { params });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区场景作者信息失败')
  }
}

export async function fetchLearningDynamicApi(params: any) {
  try {
    const response = await api.get('/java/ground/aix/area/get_area_sign_up_sort', { params });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区学习动态失败')
  }
}

export async function fetchAreaOpusApi(params: any) {
  try {
    const response = await api.get('/java/ground/aix/opus/get_opus_ground', { params });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区作品失败')
  }
}

export async function fetchScoreBoardDataApi(params: any) {
  try {
    const response = await api.get('/java/ground/aix/area/get_area_work_score_sort', { params });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区作品排行榜数据失败')
  }
}

export async function fetchAreaAchievementsApi(params: any) {
  try {
    const response = await api.get('/java/ground/aix/result/get_results', { params });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区成果失败')
  }
}

export async function createSceneApi(
  sceneTitle: string,
  reward: string,
  tags: string,
  achievement: string,
  areaId: string,
  startTime: string,
  endTime: string,
  coverImage: File,
) {
  try {
    const formData = new FormData();
    formData.append('coverImage', coverImage);
    const response = await api.post('/java/ground/aix/scene/insert_scene', formData, {
      params: {
        sceneTitle,
        reward,
        tags,
        achievement,
        areaId,
        startTime,
        endTime,
      },
    });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('创建微场景失败')
  }
}

export const updateSceneApi = async (
  sceneId: number,
  sceneTitle: string,
  reward: string,
  tags: string,
  achievement: string,
  areaId: string,
  startTime: string,
  endTime: string,
) => {
  const response = await api.post('/java/ground/aix/scene/update_scene_card', {
    sceneId,
    sceneTitle,
    reward,
    tags: tags.split(','),
    achievement: achievement.split(','),
    areaId,
    startTime,
    endTime,
  });
  return response.data;
};

export const deleteSceneApi = async (sceneId: string) => {
  try {
    const response = await api.post(`/java/ground/aix/scene/delete_scene/${sceneId}`);
    return response.data;
  }
  catch (error) {
    throw new Error('删除微场景失败');
  }
};

export const updateActivityApi = async (params: {
  activityId: string;
  activityTitle: string;
  introduction: string;
  authors: string;
  areaId: string;
  targetUrl: string;
  tags: string[];
}) => {
  try {
    const response = await api.post('/java/ground/aix/activity/update_activity', params);
    return response.data;
  }
  catch (error) {
    throw new Error('更新活动失败');
  }
};

export const deleteActivityApi = async (activityId: string) => {
  try {
    const response = await api.post(`/java/ground/aix/activity/delete_activity/${activityId}`);
    return response.data;
  }
  catch (error) {
    throw new Error('删除活动失败');
  }
};

export const createCourseApi = async (
  courseTitle: string,
  introduction: string,
  areaId: string,
  tags: string,
  authors: string,
  targetUrl: string,
  coverImage: File,
) => {
  try {
    const formData = new FormData();
    formData.append('coverImage', coverImage);
    const response = await api.post('/java/ground/aix/course/insert_course', formData, {
      params: {
        courseTitle,
        introduction,
        areaId,
        tags,
        authors,
        targetUrl,
      },
    });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('创建课程失败')
  }
};

export const createActivityApi = async (
  activityTitle: string,
  introduction: string,
  authors: string,
  areaId: string,
  targetUrl: string,
  tags: string,
  coverImage: File,
) => {
  try {
    const formData = new FormData();
    formData.append('coverImage', coverImage);
    const response = await api.post('/java/ground/aix/activity/insert_activity', formData, {
      params: {
        activityTitle,
        introduction,
        authors,
        areaId,
        targetUrl,
        tags,
      },
    });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('创建活动失败')
  }
};

export const updateCourseApi = async (params: {
  courseId: string;
  courseTitle: string;
  introduction: string;
  authors: string;
  areaId: string;
  targetUrl: string;
  tags: string[];
}) => {
  try {
    const response = await api.post('/java/ground/aix/course/update_course', params);
    return response.data;
  }
  catch (error) {
    throw new Error('更新课程失败');
  }
};

export const deleteCourseApi = async (courseId: string) => {
  try {
    const response = await api.post(`/java/ground/aix/course/delete_course/${courseId}`);
    return response.data;
  }
  catch (error) {
    throw new Error('删除课程失败');
  }
};

export const createProjectApi = async (
  projectTitle: string,
  introduction: string,
  authors: string,
  areaId: string,
  targetUrl: string,
  tags: string,
  coverImage: File,
) => {
  try {
    const formData = new FormData();
    formData.append('coverImage', coverImage);
    const response = await api.post('/java/ground/aix/project/insert_project', formData, {
      params: {
        projectTitle,
        introduction,
        authors,
        areaId,
        targetUrl,
        tags,
      },
    });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('创建项目失败')
  }
};

export const getCurrentUserApi = async () => {
  try {
    const response = await api.get('/java/user/whoami_frontend');
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取当前用户信息失败')
  }
};

export const updateProjectApi = async (params: {
  projectId: number;
  projectTitle: string;
  introduction: string;
  aiXProjectAuthors: string[];
  areaId: number;
  targetUrl: string;
  aiXProjectTags: string[];
}) => {
  try {
    const response = await api.post('/java/ground/aix/project/update_project', params);
    return response.data;
  }
  catch (error) {
    throw new Error('更新项目失败');
  }
};

export const deleteProjectApi = async (projectId: string) => {
  try {
    const response = await api.post(`/java/ground/aix/project/delete_project/${projectId}`);
    return response.data;
  }
  catch (error) {
    throw new Error('删除项目失败');
  }
};

export const updateSceneCoverApi = async (sceneId: number, areaId: number, file: File) => {
  try {
    const formData = new FormData();
    formData.append('file', file);
    const response = await api.post(
      `/java/ground/aix/scene/update_scene_cover?sceneId=${sceneId}&areaId=${areaId}`,
      formData,
      {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      },
    );
    return response.data;
  }
  catch (error) {
    throw new Error('更新场景封面失败');
  }
};

export const updateActivityCoverApi = async (activityId: number, areaId: number, file: File) => {
  try {
    const formData = new FormData();
    formData.append('file', file);
    const response = await api.post(
      `/java/ground/aix/activity/update_activity_cover?activityId=${activityId}&areaId=${areaId}`,
      formData,
      {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      },
    );
    return response.data;
  }
  catch (error) {
    throw new Error('更新活动封面失败');
  }
};

export const updateCourseCoverApi = async (courseId: number, areaId: number, file: File) => {
  try {
    const formData = new FormData();
    formData.append('file', file);
    const response = await api.post(
      `/java/ground/aix/course/update_course_cover?courseId=${courseId}&areaId=${areaId}`,
      formData,
      {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      },
    );
    return response.data;
  }
  catch (error) {
    throw new Error('更新课程封面失败');
  }
};

export const updateProjectCoverApi = async (projectId: number, areaId: number, file: File) => {
  try {
    const formData = new FormData();
    formData.append('file', file);
    const response = await api.post(
      `/java/ground/aix/project/update_project_cover?projectId=${projectId}&areaId=${areaId}`,
      formData,
      {
        headers: {
          'Content-Type': 'multipart/form-data',
        },
      },
    );
    return response.data;
  }
  catch (error) {
    throw new Error('更新项目封面失败');
  }
};

export const createAchievementApi = async (
  resultTitle: string,
  introduction: string,
  areaId: string,
  coverImage: File,
) => {
  try {
    const formData = new FormData();
    formData.append('coverImage', coverImage);
    const response = await api.post('/java/ground/aix/result/insert_result', formData, {
      params: {
        resultTitle,
        introduction,
        areaId,
      },
    });
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('创建成果失败')
  }
};

export async function fetchAreaListApi() {
  try {
    const response = await api.get('/java/ground/aix/area/get_area_list');
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区列表失败')
  }
}

export async function fetchAreaDetailApi(areaId: string) {
  try {
    const response = await api.get(`/java/ground/aix/area/get_area/${areaId}`);
    if (response)
      return response.data;
  }
  catch (error) {
    throw new Error('获取专区详情失败')
  }
}
